﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

/// <summary>
/// Summary description for ChanelBusiness
/// </summary>
public class ChanelBusiness
{
	public ChanelBusiness()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public bool Insert(ChanelEntities item)
    {
        string[] para = { "@channelName", "@channelLink", "@channelParentID", "@channelLocked", "@channelLanguage", "@channelRequireApprove" };
        string[] values = { item._channelName, item._channelLink, item._channelParentID.ToString(), item._channelLocked.ToString(), item._channelLanguage.ToString(), item._channelRequireApprove.ToString() };
        if (DataAccess.getExecProcedure("sp_InsertChannel", para, values) > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public bool Insert(string[] values)
    {
        string[] para = { "@channelName", "@channelLink", "@channelParentID", "@channelLocked", "@channelLanguage", "@channelRequireApprove" };
         
        if (DataAccess.getExecProcedure("sp_InsertChannel", para, values) > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public bool Edit(ChanelEntities item)
    {
        string[] para = { "@channelID", "@channelName", "@channelParentID", "@channelLocked" };
        string[] values = { item._channelID.ToString(),item._channelName,item._channelParentID.ToString(),item._channelLocked.ToString() };
        if (DataAccess.getExecProcedure("sp_UpdateChannel", para, values) > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public bool Edit(string[] values)
    {
        string[] para = { "@channelID", "@channelName", "@channelParentID", "@channelLocked" };
        
        if (DataAccess.getExecProcedure("sp_UpdateChannel", para, values) > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public bool Delete(int id)
    {
        string[] para = { "@id" };
        string[] values = { id.ToString() };
        if (DataAccess.getExecProcedure("DeleteChannel", para, values) > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    public ArrayList showMenu()
    {
        ArrayList items=new ArrayList();
        ChanelEntities item = null;
        SqlDataReader dr= DataAccess.DataReader_getReader("exec sp_ShowMenu");
        if (dr.HasRows)
        {
            while (dr.Read())
            {
                item = new ChanelEntities();
                string test = dr["channelID"].ToString();
                item._channelID = int.Parse(dr["channelID"].ToString());
                item._channelParentID = int.Parse(dr["channelParentID"].ToString());
                item._channelName = dr["channelName"].ToString();
                item._channelLink = dr["channelLink"].ToString();
                item._channelPriority = int.Parse(dr["channelPriority"].ToString());
                item._channelLocked = dr["channelLocked"].ToString();
                item._channelLanguage = int.Parse(dr["channelLanguage"].ToString());
                item._channelRequireApprove = dr["channelRequireApprove"].ToString();
                items.Add(item);
                item = null;
            }
        }
        return items;
    }

    public DataTable getAllChannel()
    {
        DataTable dt = DataAccess.DataTable_Query("exec sp_GetChannel '0'");
        return dt;
    }
    public ChanelEntities getChannel(int id)
    {
        DataTable dt = DataAccess.DataTable_Query("exec sp_GetChannel '" + id.ToString() + "'");
        ChanelEntities item = null;
        if (dt != null)
        {
            item = new ChanelEntities();
            DataRow row = dt.Rows[0];
            item._channelID = int.Parse(row["channelID"].ToString());
            item._channelParentID = int.Parse(row["channelParentID"].ToString());
            item._channelName = row["channelName "].ToString();
            item._channelLink = row["channelLink "].ToString();
            item._channelPriority = int.Parse(row["channelPriority"].ToString());
            item._channelLocked = row["channelLocked"].ToString();
        }
        return item;
    }
    public int max(int parent)
    {
        return (int)DataAccess.getScalar("exec sp_MaxChannelParent '" + parent.ToString() + "'");
    }
    public int min(int parent)
    {
        return (int)Database.getScalar("exec sp_MinChannelParent '" + parent.ToString() + "'");
    }
}